﻿using System.Collections.Generic;
using System.ComponentModel;
using GIMS.Def.Arguments;
using System.Diagnostics.Contracts;
using System;
using System.Linq;

namespace GIMS.GUI.ArgumentControls
{
    public class GeometryArgumentControlViewModel : INotifyPropertyChanged
    {
        private GeometryArgumentControlViewModel()
        {
            //needed for unit tests
        }

        private GeometryType currentGeometryType;
        public event PropertyChangedEventHandler PropertyChanged;

        public GeometryArgumentControlViewModel(GeometryArgument geometryArgument)
        {
            Contract.Requires(null != geometryArgument);
            GeometryArgument = geometryArgument;
            GeometryArgument.PropertyChanged += new PropertyChangedEventHandler(GeometryArgument_PropertyChanged);
            InitGeometryTypeValues();
        }

        private void GeometryArgument_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "SelectedGeometryType")
            {
                OnPropertyChanged("IsPercent");
                OnPropertyChanged("IsxyPercent");
                OnPropertyChanged("IsWidth");
                OnPropertyChanged("IsHeigth");
                OnPropertyChanged("IsArea");
                OnPropertyChanged("IsOffsetNeeded");
            }
        }

        public IEnumerable<GeometryTypeRepresenter> GeometryTypeRepresenters
        {
            get;
            private set;
        }

        public bool HasMoreThenOneGeometryType
        {
            get { return GeometryTypeRepresenters.Count() > 1; }
        }

        public bool IsPercent
        {
            get { return GeometryArgument.SelectedGeometryType == GeometryType.percent; }
        }

        public bool IsxyPercent
        {
            get { return GeometryArgument.SelectedGeometryType == GeometryType.xypercent; }
        }

        public bool IsWidth
        {
            get { return GeometryArgument.SelectedGeometryType == GeometryType.width || (GeometryArgument.SelectedGeometryType & GeometryType.widthxheight) == GeometryType.widthxheight; }
        }

        public bool IsHeigth
        {
            get { return GeometryArgument.SelectedGeometryType == GeometryType.height || (GeometryArgument.SelectedGeometryType & GeometryType.widthxheight) == GeometryType.widthxheight; }
        }

        public  bool IsArea
        {
            get { return GeometryArgument.SelectedGeometryType == GeometryType.area; }
        }

        public bool IsOffsetNeeded
        {
            get
            {
                return GeometryArgument.SelectedGeometryType == (GeometryType.widthxheight | GeometryType.offsetNeeded);
            }
        }

        public GeometryArgument GeometryArgument
        {
            get;
            private set;
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void InitGeometryTypeValues()
        {
            var geometryTypeValues = new List<GeometryTypeRepresenter>();
            var values = Enum.GetValues(typeof(GeometryType));
            foreach (var value in values)
            {
                if ((GeometryType)value != GeometryType.offsetNeeded
                    && GeometryArgument.SupportedGeometryTypes.HasFlag((GeometryType)value))
                {
                    var geometryTypeValue = new GeometryTypeRepresenter
                    {
                        Name = Enum.GetName(typeof(GeometryType), value),
                        Value = (GeometryType)value
                    };
                    geometryTypeValues.Add(geometryTypeValue);
                }
            }
            GeometryTypeRepresenters = geometryTypeValues;
        }
    }
}
